package report

import user.AuthController;

class PlanController extends AuthController{

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

    def planService                       
                             
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [planInstanceList: Plan.list(params), planInstanceTotal: Plan.count()]
    }

    def create = {
    	if  (hasPermission()) {
    		def planInstance = new Plan()
    		planInstance.properties = params
    		return [planInstance: planInstance]
    	} else {
    		flash.message = "Você não está autorizado a acessar a página requisitada anteriormente."
    		redirect(controller:"plan", action: "list")
    	}
    }

    def save = {
        def planInstance = new Plan(params)
        if (hasPermission()) {
        	 if (planService.save(planInstance)) {
                 flash.message = "${message(code: 'default.created.message', args: [message(code: 'plan.label', default: 'Plan'), planInstance.id])}"
                 redirect(action: "show", id: planInstance.id)
             }
             else {
                 render(view: "create", model: [planInstance: planInstance])
             }
        } else {
        	flash.message = "Você não está autorizado a acessar a página requisitada anteriormente."
    		redirect(controller:"plan", action: "list")
        }
       
    }

    def show = {
        def planInstance = Plan.get(params.id)
        if (!planInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'plan.label', default: 'Plan'), params.id])}"
            redirect(action: "list")
        }
        else {
            [planInstance: planInstance]
        }
    }

    def edit = {
        def planInstance = Plan.get(params.id)
        if (hasPermission()) {
        	if (!planInstance) {
                flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'plan.label', default: 'Plan'), params.id])}"
                redirect(action: "list")
            }
            else {
                return [planInstance: planInstance]
            }
        } else {
        	flash.message = "Você não está autorizado a acessar a página requisitada anteriormente."
    		redirect(controller:"plan", action: "list")
        }
        
    }

    def update = {
        def planInstance = Plan.get(params.id)
        if (hasPermission()) {
        	if (planInstance) {
        		if (params.version) {
        			def version = params.version.toLong()
        			if (planInstance.version > version) {
        				
        				planInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'plan.label', default: 'Plan')] as Object[], "Another user has updated this Plan while you were editing")
        				render(view: "edit", model: [planInstance: planInstance])
        				return
        			}
        		}
        		planInstance.properties = params
        		if (!planInstance.hasErrors() && planService.save(planInstance)) {
        			flash.message = "${message(code: 'default.updated.message', args: [message(code: 'plan.label', default: 'Plan'), planInstance.id])}"
        				redirect(action: "show", id: planInstance.id)
        		}
        		else {
        			render(view: "edit", model: [planInstance: planInstance])
        		}
        	}
        	else {
        		flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'plan.label', default: 'Plan'), params.id])}"
    			redirect(action: "list")
        	}
        } else {
        	flash.message = "Você não está autorizado a acessar a página requisitada anteriormente."
    		redirect(controller:"plan", action: "list")
        }
    }

    def delete = {
        def planInstance = Plan.get(params.id)
        if (hasPermission()) {
        	if (planInstance) {
        		try {
        			planService.delete(planInstance)
        			flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'plan.label', default: 'Plan'), params.id])}"
        				redirect(action: "list")
        		}
        		catch (org.springframework.dao.DataIntegrityViolationException e) {
        			flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'plan.label', default: 'Plan'), params.id])}"
        				redirect(action: "show", id: params.id)
        		}
        	}
        	else {
        		flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'plan.label', default: 'Plan'), params.id])}"
        			redirect(action: "list")
        	}
        	
        } else{
        	flash.message = "Você não está autorizado a acessar a página requisitada anteriormente."
    		redirect(controller:"plan", action: "list")
        }
    }
}
